package com.thinkingstar.iads.datacenter.entity.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.thinkingstar.iads.datacenter.entity.DcProjectDesignConstantVersion;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.trans.Atom;
import org.nutz.trans.Trans;

import com.bstek.urule.model.rule.lhs.And;
import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.CommonDateUtil;
import com.thinkingstar.iads.common.utils.SwapUtil;
import com.thinkingstar.iads.datacenter.entity.DcDesignContentMajor;
import com.thinkingstar.iads.datacenter.entity.DcProjectMentionInformation;
import com.thinkingstar.iads.datacenter.entity.DcProjectMentionInformationContentMajor;

/**
 * @ClassName: DaoDcProjectDesignType 
 * @Description:数据中心_项目_项目提入资料信息维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcProjectMention extends EntityRepositoryServiceImpl<DcProjectMentionInformation>{

	public DaoDcProjectMention(Dao dao) {
		super(dao);
	}
	
	/**
	 * 更新对象
	 * @param dcProjectMentionInformation
	 * @return
	 */
	public int updateProjectMention(DcProjectMentionInformation dcProjectMentionInformation){
		return updateObj(dcProjectMentionInformation);
	}
	
	/**
	 * 逻辑删除
	 * @return
	 * @throws Exception 
	 */
	public int delProjectMention(String id) throws Exception{
		return delObj(id);
	}
	
	
	public DcProjectMentionInformation findObjByName(String name){
		Criteria criteria =Cnd.NEW();
		criteria.where().and("del_flag", "=", false).and("mention_information_code", "=", name);
		List <DcProjectMentionInformation> result = query(criteria);
		return !result.isEmpty()?result.get(0):null;
	}
	
	//查询动车机务设施的相关资料
	public List<DcProjectMentionInformation> findDcjwssByProjectId(String project_id){
		String find_sql = " select dpmi.* from "
				+ " dc_project_mention_information_content_major dpmicm "
				+ " inner join dc_project_mention_information dpmi on dpmicm.project_mention_information_id = dpmi.id "
				+ " and dpmicm.del_flag = '0' and dpmi.del_flag = '0'"
				+ " and dpmicm.project_design_phase_id = (select project_design_phase_id from pc_project_info where id = @project_id and del_flag = '0')"
				+ " and dpmicm.content_id = (select project_design_content_id from pc_project_info where id = @project_id and del_flag = '0')"
				+ " and dpmicm.major_id = (select id from dc_project_design_major where project_design_major_name like '%动车/机务设施%' and del_flag = '0')";
		Sql sql = Sqls.create(find_sql);
		sql.params().set("project_id", project_id);
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List <DcProjectMentionInformation> list = new ArrayList<>();
				while (rs.next()) {
					DcProjectMentionInformation dcProjectMentionInformation = new DcProjectMentionInformation();
					dcProjectMentionInformation.setId(rs.getString("id"));
					dcProjectMentionInformation.setMajor_id(rs.getString("major_id"));
					dcProjectMentionInformation.setMention_information_code(rs.getString("mention_information_code"));
					dcProjectMentionInformation.setMention_information_name(rs.getString("mention_information_name"));
					dcProjectMentionInformation.setCreate_time(rs.getString("create_time"));
					dcProjectMentionInformation.setUpdate_time(rs.getString("update_time"));
					dcProjectMentionInformation.setMention_information_type(rs.getBoolean("mention_information_type"));
					dcProjectMentionInformation.setOrder_num(rs.getString("order_num"));
					list.add(dcProjectMentionInformation);
				}
				return list;
			}
		});
		dao().execute(sql);
		return sql.getList(DcProjectMentionInformation.class);
	}
	
	
	//查询某项目的设计动车实施资料信息
	public List<DcProjectMentionInformation> findSjdcssByProjectId(String project_id){
		String find_sql = " select dpmi.* from "
				+ " dc_project_mention_information_content_major dpmicm "
				+ " inner join dc_project_mention_information dpmi on dpmicm.project_mention_information_id = dpmi.id "
				+ " and dpmicm.del_flag = '0' and dpmi.del_flag = '0'"
				+ " and dpmicm.project_design_phase_id = (select project_design_phase_id from pc_project_info where id = @project_id and del_flag = '0')"
				+ " and dpmicm.content_id = (select project_design_content_id from pc_project_info where id = @project_id and del_flag = '0')"
				+ " and dpmicm.major_id = (select id from dc_project_design_major where project_design_major_name like '%动车设施%' and del_flag = '0')"
				+ " and dpmi.mention_information_name like '%动车设施%'";
		Sql sql = Sqls.create(find_sql);
		sql.params().set("project_id", project_id);
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List <DcProjectMentionInformation> list = new ArrayList<>();
				while (rs.next()) {
					DcProjectMentionInformation dcProjectMentionInformation = new DcProjectMentionInformation();
					dcProjectMentionInformation.setId(rs.getString("id"));
					dcProjectMentionInformation.setMajor_id(rs.getString("major_id"));
					dcProjectMentionInformation.setMention_information_code(rs.getString("mention_information_code"));
					dcProjectMentionInformation.setMention_information_name(rs.getString("mention_information_name"));
					dcProjectMentionInformation.setCreate_time(rs.getString("create_time"));
					dcProjectMentionInformation.setUpdate_time(rs.getString("update_time"));
					dcProjectMentionInformation.setMention_information_type(rs.getBoolean("mention_information_type"));
					dcProjectMentionInformation.setOrder_num(rs.getString("order_num"));
					list.add(dcProjectMentionInformation);
				}
				return list;
			}
		});
		dao().execute(sql);
		return sql.getList(DcProjectMentionInformation.class);
	}
	
	/**
	 * 根据条件获取提入资料信息
	 * @param nutMap
	 * @return
	 */
	public QueryResult getPageListByCondition(NutMap nutMap){
		List <Map> page_list = null;
		Pager pager = null;
		String find_sql = " select dpmi.* , dpdm.project_design_major_name "
				        + " from dc_project_mention_information dpmi left join dc_project_design_major dpdm on dpmi.major_id = dpdm.id"
				        + " where dpmi.del_flag = '0' and dpdm.del_flag = '0' ";
		if(nutMap.get("mention_code") != null){
			find_sql += " and dpmi.mention_information_code = @mention_code ";
		}
		if(nutMap.get("mention_name") != null){
			find_sql += " and dpmi.mention_information_name like @mention_name ";
		}
		if(nutMap.get("mention_type") != null){
			find_sql += " and dpmi.mention_information_type = @mention_type ";
		}
		if(nutMap.get("major_name") != null){
			find_sql += " and dpdm.project_design_major_name = @major_name ";
		}
		find_sql += " order by dpmi.create_time desc ";
		Sql sql = Sqls.create(find_sql);
		if(nutMap.get("mention_code") != null){
			sql.params().set("mention_code", nutMap.get("mention_code"));
		}
		if(nutMap.get("mention_name") != null){
			sql.params().set("mention_name", "%"+nutMap.get("mention_name")+"%");
		}
		if(nutMap.get("mention_type") != null){
			sql.params().set("mention_type", nutMap.get("mention_type"));
		}
		if(nutMap.get("major_name") != null){
			sql.params().set("major_name", "%"+nutMap.get("major_name")+"%");
		}
		if(nutMap.get("page_num") != null && nutMap.get("page_size") != null){
			pager = new Pager(nutMap.getInt("page_num"),nutMap.getInt("page_size"));
			pager.setRecordCount(this.getMentionsCountByCondition(nutMap));
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			List<Map> list = new LinkedList<>();
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next()){
					Map<String,Object> map = new HashMap<>();
					ResultSetMetaData metaData = rs.getMetaData();
					int count = metaData.getColumnCount();
					for (int i = count; i > 0; i--) {
						String filed = metaData.getColumnName(i);
						map.put(filed, rs.getString(filed));
					}
					list.add(map);
				}
				//SwapUtil.clearNull(list);
				return list;
			}
		});
		dao().execute(sql);
		page_list = sql.getList(Map.class);
		return new QueryResult(page_list,pager);
	}
	
	/**
	 * 根据条件查询记录数
	 * @return
	 */
	public int getMentionsCountByCondition(NutMap nutMap){
		int count = 0;
		String find_sql = " select count(dpmi.id) size_num "
		        + " from dc_project_mention_information dpmi left join dc_project_design_major dpdm on dpmi.major_id = dpdm.id"
		        + " where dpmi.del_flag = '0' and dpdm.del_flag = '0' ";
		if(nutMap.get("mention_code") != null){
			find_sql += " and dpmi.mention_information_code = @mention_code ";
		}
		if(nutMap.get("mention_name") != null){
			find_sql += " and dpmi.mention_information_name like @mention_name ";
		}
		if(nutMap.get("mention_type") != null){
			find_sql += " and dpmi.mention_information_type = @mention_type ";
		}
		if(nutMap.get("major_name") != null){
			find_sql += " and dpdm.project_design_major_name = @major_name ";
		}
		Sql sql = Sqls.create(find_sql);
		if(nutMap.get("mention_code") != null){
			sql.params().set("mention_code", nutMap.get("mention_code"));
		}
		if(nutMap.get("mention_name") != null){
			sql.params().set("mention_name", "%"+nutMap.get("mention_name")+"%");
		}
		if(nutMap.get("mention_type") != null){
			sql.params().set("mention_type", nutMap.get("mention_type"));
		}
		if(nutMap.get("major_name") != null){
			sql.params().set("major_name", "%"+nutMap.get("major_name")+"%");
		}
		sql.setCallback(new SqlCallback() {
			int count = 0;
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next())
				count = rs.getInt("size_num");
				return count;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}

	/**
	 * 根据条件(阶段id,专业id,设计内容id)查询资料
	 * @param nutMap
	 * @return
	 */
	public List<Map> getMentionListByCondition(NutMap nutMap){
		List <Map> list = null;
		String find_sql = " select dpmi.* , "
				   + " ifnull((select dpmicm.id from dc_project_mention_information_content_major dpmicm where dpmicm.project_mention_information_id = dpmi.id and dpmicm.del_flag = false "
				   + " and dpmicm.project_design_phase_id = @phase_id and dpmicm.content_id = @content_id and dpmicm.major_id = @major_id ),'') record_id"
				   + " from dc_project_mention_information dpmi where dpmi.del_flag = false and dpmi.major_id = @dpmi_major_id";
		Sql sql = Sqls.create(find_sql);
		sql.params().set("phase_id", nutMap.get("phase_id")).set("content_id", nutMap.get("content_id")).set("major_id", nutMap.get("major_id")).set("dpmi_major_id", nutMap.get("major_id"));
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List <Map <String , Object>> list = new ArrayList<>();;
				while (rs.next()) {
					Map <String , Object> map = new HashMap<>();
					map.put("id", rs.getString("id"));
					map.put("mention_name", rs.getString("mention_information_name"));
					map.put("record_id", rs.getString("record_id"));
					list.add(map);
				}
				return list;
			}
		});
		dao().execute(sql);
		return sql.getList(Map.class);
	}
	
	public int updateMentionsByContent(NutMap nutMap){
		String content_id = nutMap.getString("content_id");
		String major_id = nutMap.getString("major_id");
		Trans.exec(new Atom() {
			@Override
			public void run() {
				String [] mention_arr = nutMap.getString("mention_str").split(",");
				String phase_id = nutMap.getString("phase_id");
				delMentionsByContent(major_id,content_id,phase_id);
				for(String mention_id : mention_arr){
					DcProjectMentionInformationContentMajor add_obj = new DcProjectMentionInformationContentMajor();
					add_obj.setCreate_time(CommonDateUtil.getDateTime());
					add_obj.setMajor_id(major_id);
					add_obj.setContent_id(content_id);
					add_obj.setProject_design_phase_id(phase_id);
					add_obj.setProject_mention_information_id(mention_id);
					dao().insert(add_obj);
				}
			}
		});
		return 1; 
	}
	
	public Boolean judgeRepeatedCommit(String mention_code){
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", false).and("mention_information_code", "=", mention_code);
		List <DcProjectMentionInformation> list = query(criteria);
		if(!list.isEmpty()){
			return false;
		}
		return true;
	}
	
	//清除某阶段某设计内容在某专业下的互提资料
	public void delMentionsByContent(String major_id , String content_id , String phase_id){
		String del_sql = " delete from dc_project_mention_information_content_major where del_flag = '0' and content_id = @content_id "
				       + " and major_id = @major_id  and project_design_phase_id = @phase_id ";
		Sql sql = Sqls.create(del_sql);
		sql.params().set("content_id", content_id).set("phase_id", phase_id).set("major_id", major_id);
		dao().execute(sql);
	}
	
	//查询某设计内容匹配的相关专业以及提入资料信息
	public List <DcProjectMentionInformation> getMentionDataByContentAndMajor(String content_strs , String major_id , String design_phase_id){
		String fin_sql = "select distinct(dpmi.id), dpmi.mention_information_code , dpmi.mention_information_name , dpmi.mention_information_type ,dpmi.order_num "
				+ " from dc_project_mention_information_content_major dpmicm left join dc_project_mention_information dpmi on dpmicm.project_mention_information_id = dpmi.id "
				+ " where dpmicm.content_id in "+content_strs+" and dpmicm.major_id = @major_id "
				+ " and dpmicm.del_flag = '0' and dpmi.del_flag = '0' and dpmicm.project_design_phase_id = @phase_id"
				+ " order by dpmi.order_num+0 " ;
		Sql sql = Sqls.create(fin_sql);
		sql.params().set("major_id", major_id);
		sql.params().set("phase_id", design_phase_id);
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List <DcProjectMentionInformation> list = new LinkedList<>();
				while (rs.next()) {
					DcProjectMentionInformation add_obj = new DcProjectMentionInformation();
					add_obj.setId(rs.getString("id"));
					add_obj.setMention_information_code(rs.getString("mention_information_code"));
					add_obj.setMention_information_name(rs.getString("mention_information_name"));
					add_obj.setMention_information_type(rs.getBoolean("mention_information_type"));
					list.add(add_obj);
				}
				return list;
			}
		});
		dao().execute(sql);
		return sql.getList(DcProjectMentionInformation.class);
	}
	/**	根据设计阶段和设计内容 查询 所有的提入资料 非结构化 */
	public List<NutMap> getMentionFjghByPhaseAndContentId(String phase_id,String content_id) {
		List <NutMap> list = null;
		String fin_sql = "select i.* from dc_project_mention_information i ,dc_project_mention_information_content_major m where i.id=m.project_mention_information_id and " +
				"m.project_design_phase_id='"+phase_id+"' and m.content_id='"+content_id+"' and i.del_flag='0' and m.del_flag='0' and i.mention_information_type='0';" ;
		Sql sql = Sqls.create(fin_sql);
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<NutMap> list = new ArrayList<>();
				while (rs.next()){
					NutMap nutMap = new NutMap();
					ResultSetMetaData metaData = rs.getMetaData();
					int count = metaData.getColumnCount();
					for (int i = count; i > 0; i--) {
						String filed = metaData.getColumnName(i);
						nutMap.put(filed, rs.getString(filed));
					}
					list.add(nutMap);
				}
				return list;
			}
		});
		dao().execute(sql);
		list = sql.getList(NutMap.class);
		return list;
	}
	/**	根据设计阶段和设计内容 查询 所有的提入资料 结构化 */
	public List<NutMap> getMentionJghByPhaseAndContentId(String phase_id,String content_id) {
		List <NutMap> list = null;
		String fin_sql = "select i.* from dc_project_mention_information i ,dc_project_mention_information_content_major m where i.id=m.project_mention_information_id and " +
				"m.project_design_phase_id='"+phase_id+"' and m.content_id='"+content_id+"' and i.del_flag='0' and m.del_flag='0' and i.mention_information_type='1';" ;
		Sql sql = Sqls.create(fin_sql);
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<NutMap> list = new ArrayList<>();
				while (rs.next()){
					NutMap nutMap = new NutMap();
					ResultSetMetaData metaData = rs.getMetaData();
					int count = metaData.getColumnCount();
					for (int i = count; i > 0; i--) {
						String filed = metaData.getColumnName(i);
						nutMap.put(filed, rs.getString(filed));
					}
					list.add(nutMap);
				}
				return list;
			}
		});
		dao().execute(sql);
		list = sql.getList(NutMap.class);
		return list;
	}
}
